<template>
  <view class="address_add_box">
    <form @submit="formSubmit">
      <view>
        <view class="list_item">
          <view class="title">收货人姓名</view>
          <view class="input">
            <input type="text" class="text_font" @input="receiverName" :value="defaultAddress.receiverName"
              placeholder-style="color:#c6c6c6;" placeholder="请输入" />
          </view>
        </view>
        <view class="list_item">
          <view class="title">联系电话</view>
          <view class="input">
            <input type="number" class="text_fontnum" @input="mobile" maxlength="11" :value="defaultAddress.mobile"
              placeholder-style="color:#c6c6c6;" placeholder="请输入" />
          </view>
        </view>
        <view class="list_item list_item_address" @click="openAddressPickerNew" :data-index="3">
          <view class="title" @click.stop="stopClick" style="width: 140rpx;">所在地区</view>
          <view class="input" style="line-height: 40rpx;">
            <view :style="{color: datalist[0] == '请选择省' ? '#c6c6c6': '#757575'}">
              <text>{{ datalist[0] == '请选择省' ? '省' :  datalist[0].areaName }}</text>
              <text class="separate-tag"> - </text>
              <text>{{ datalist[1] == '请选择市' ? '市' :  datalist[1].areaName }}</text>
              <text class="separate-tag"> - </text>
              <text>{{ datalist[2] == '请选择区' ? '区' :  datalist[2].areaName }}</text>
              <text class="separate-tag" v-if="datalist.length > 3"> - </text>
              <text>{{ datalist[3] == '请选择街道' ? '街道' :  datalist[3].areaName }}</text>
            </view>
          </view>
          <image class="right_icon"
            src="https://100000063-1252208446.file.myqcloud.com/images/macallan-remvp/right_icon.png"></image>
        </view>
        <view class="list_item">
          <view class="title">详细地址</view>
          <view class="input">
            <input type="text" class="text_font" @input="addressDetail" :value="defaultAddress.addressDetail"
              placeholder-style="color:#c6c6c6;" placeholder="请输入" />
          </view>
        </view>
        <view class="list_item" style="border: none;">
          <view class="title">设置为默认</view>
          <view class="input" @click="changeCheckBoxState">
            <image v-if="isDefult" class="defult_img"
              src="https://100000063-1252208446.file.myqcloud.com/images/macallan-remvp/sel_img.png"></image>
            <image v-else class="defult_img"
              src="https://100000063-1252208446.file.myqcloud.com/images/macallan-remvp/nosel_img.png"></image>
          </view>
        </view>
        <image class="line_img"
          src="https://100000063-1252208446.file.myqcloud.com/images/macallan-remvp/empty_cart_line3x_981.png"></image>
        <!-- <view class="weixin_get_address">
          <view class="qAdd" @click="getAddress" v-if="!showgetAddress">
            <text class="qAdd_text">微信授权地址信息</text>
          </view>
          <view class="qAdd" @click="changeCurrent" v-else>
            <text class="qAdd_text">选择收货地址</text>
          </view>
        </view> -->
      </view>
      <view class="bottom_box">
        <view class="tipBox" v-if="type !== 'draw'" style="text-align: center;">
          温馨提示：请您不要将未成年人作为订单收件人或让未成年人接收或接触所购买的酒类商品。
        </view>
        <view class="tipBox" v-else>
          <view class="labeltext">温馨提示：请您不要将未成年人作为订单收件人，或让未成年人接收或接触所购买的酒类商品。</view>
          <view class="labeltext">请仔细核对您的收货地址信息，参与成功后的购买资格仅对该登记地址有效。</view>
          <view class="labeltext">
            提交抽签购活动的登记申请并不代表获得购买资格，也不构成您和我们订立相关商品购买合同的要约或者承诺，您的登记申请提交后不能确保一定能够成功通过并获取购买资格。
          </view>
        </view>
        <!-- 抽签购地址提交按钮 -->
        <view class="btn_box" v-if="type == 'draw'" @click="formSubmit">
          <button class="btnStyle" :class="activityBtnShow ? '': 'btnStyle_not'">确认</button>
        </view>
        <view class="btn_box" v-else-if="type == 'gift'" @click="formSubmit">
          <button class="btnStyle" :class="activityBtnShow ? '': 'btnStyle_not'">确认</button>
        </view>
        <view class="btn_box" v-else>
          <button class="btnStyle" :class="activityBtnShow ? '': 'btnStyle_not'"
            @click="$preventReClick(formSubmit)">确认</button>
        </view>
      </view>
      <checkDialog v-if="dialog" :address='address' @checkIn='checkIn' @closeDialog='closeDialog'></checkDialog>
      <districtDialog v-if="district" @closeDistrictDialog='closeDistrictDialog'></districtDialog>
      <view class="add-address-mask" v-if="popup" @touchmove.stop.prevent="moveHandle">
        <view class="content_box">
          <!-- stepsList步骤条样式显示省市区 -->
          <stepsList :datalist="datalist" :isIos="isIos" :addressindex="addressindex" @click="clickShowCityList" :showIndex="showIndex"
            @closeMask="closeMask" @clickCancel="clickCancel">
          </stepsList>
          <view style="position: relative;":class="datalist[0] == '请选择省' ? 'max-height' : datalist[1] == '请选择市' && datalist[2] == '请选择区' ? 'sec-height' : datalist[2] == '请选择区' && datalist.length == 4 || datalist.length == 3 ? 'third-height' : 'fouth-height'">
            <uni-indexed-list :options="list" :addressindex="addressindex" :clickAreaCode.sync="clickAreaCode"
              :clickAreaName="clickAreaName" :showSelect="false" @click="bindClick"></uni-indexed-list>
          </view>
        </view>
      </view>
    </form>
		<!-- 弱提示 弹窗 -->
		<toast-popup ref="toastPopup" @onToastPopupClick="onToastPopupClick"></toast-popup>
  </view>
</template>
<script>
  import api from '@/api/api';
  import {
    CUSTOMER_INFO,
    MERCHANT_INFO
  } from '@/utils/constant';
  import checkDialog from './drawCheckDialog.vue';
  import AreaPicker from './areaPicker';
  import districtDialog from './districtDialog.vue';
  import uniIndexedList from '@/components/uni-indexed-list/uni-indexed-list.vue';
  import stepsList from '@/components/fanxiao-address/steps-list.vue';
  import {
    buildTextData
  } from '@/common/indexed-list.js';
  export default {
    data() {
      return {
        isDefult: false,
        province: '',
        city: '',
        area: '',
        provinceCode: '',
        cityCode: '',
        areaCode: '',
        provinceName: '',
        cityName: '',
        areaName: '',
        defaultAddress: {
          receiverName: '',
          mobile: '',
          addressDetail: ''
        },
        address: {},
        type: '',
        addressId: '',
        dialog: false,
        district: false,
        popup: false,
        areaParams: null,
        list: [],
        datalist: ['请选择省', '请选择市', '请选择区', '请选择街道'],
        addressindex: 0,
        codelist: [],
        areaTypeList: ['state', 'city', 'district', 'town'],
        clickAreaCode: -1,
        clickAreaName: '',
        showIndex: -1,
        noClick: true,
        activityBtnShow: false,
      };
    },
    props: {
      showgetAddress: {
        typeof: Boolean,
        default: false
      },
      showAddList: {
        typeof: Boolean,
      },
      skuIdArr: {
        type: Array
      },
      isIos:{
        typeof: Boolean
      }
    },
    components: {
      areaPicker: AreaPicker,
      checkDialog: checkDialog,
      districtDialog: districtDialog,
      uniIndexedList: uniIndexedList,
      stepsList: stepsList,
    },
    mounted() {
      let customerInfo = uni.getStorageSync(CUSTOMER_INFO);
      this.defaultAddress.mobile = customerInfo.mobile;
      this.getMemberDetail(customerInfo.jwt);
      // this.$commonFunction.getFourthLevelAddress(1, 'state', '000000000')
    },
    methods: {
			/**
			 * 展示弱提示
			 * @param {Object} msg 文案
			 */
			handleShowTextToast(msg){
				try{
					this.$refs.toastPopup.showToast(msg);
				}catch(e){}
			},
			/**
			 * 若提示按钮点击回调
			 * @param {Object} data.type 类型：confirm确定，cancel取消
			 * @param {Object} data.callBackData 呼气弹窗时传入的数据
			 */
			onToastPopupClick(data){
				// console.log("######### data", data)
				// const {type, callBackData} = data;
			},
      moveHandle() {
        return
      },
      getcity(adcode, areaType) {
        let that = this
        let subdistrict = this.addressindex + 1;
        if (adcode == 441900000 || adcode == 442000000 || adcode == 460400000) {
          this.$commonFunction.getFourthLevelAddress(1, that.areaTypeList[that.addressindex + 1], adcode).then((
            res) => {
            if (!!areaType) {
              if (areaType == "state") {
                this.datalist = [this.datalist[0], '请选择市', '请选择区']
                this.codelist = [this.codelist[0]]
                this.showIndex = 1
              } else if (areaType == "city") {
                this.datalist = [this.datalist[0], this.datalist[1], '请选择区']
                this.codelist = [this.codelist[0], this.codelist[1]]
                this.showIndex = 2
              } else if (areaType == "district") {
                this.datalist = [this.datalist[0], this.datalist[1], this.datalist[2]]
                this.codelist = [this.codelist[0], this.codelist[1], this.codelist[2]]
                this.showIndex = 3
              }
              //  else if (areaType == "town") {
              //   this.datalist = [this.datalist[0], this.datalist[1], this.datalist[2], this.datalist[3]]
              //   this.codelist = [this.codelist[0], this.codelist[1], this.codelist[2], this.codelist[3]]
              //   this.showIndex = 4
              // }
            }
            this.list = buildTextData(res.data.content);
            this.checkFormData();
          }).catch((err) => {
 
          });
        } else {
          this.$commonFunction.getFourthLevelAddress(1, that.areaTypeList[that.addressindex], adcode).then((res) => {
            if (!!areaType) {
              if (areaType == "state") {
                this.datalist = [this.datalist[0], '请选择市', '请选择区', '请选择街道']
                this.codelist = [this.codelist[0]]
                this.showIndex = 1
              } else if (areaType == "city") {
                this.datalist = [this.datalist[0], this.datalist[1], '请选择区', '请选择街道']
                this.codelist = [this.codelist[0], this.codelist[1]]
                this.showIndex = 2
              } else if (areaType == "district") {
                this.datalist = [this.datalist[0], this.datalist[1], this.datalist[2], '请选择街道']
                this.codelist = [this.codelist[0], this.codelist[1], this.codelist[2]]
                this.showIndex = 3
              } else if (areaType == "town") {
                this.datalist = [this.datalist[0], this.datalist[1], this.datalist[2], this.datalist[3]]
                this.codelist = [this.codelist[0], this.codelist[1], this.codelist[2], this.codelist[3]]
                this.showIndex = 4
              }
            }
            this.list = buildTextData(res.data.content);
            this.checkFormData();
          }).catch((err) => {
     
          });
        }
      },
      // 点击省市区信息
      bindClick(e) {
        // this.flag = true
        this.clickAreaCode = e.clickAreaCode
        this.clickAreaName = e.clickAreaName
    
        // code存起来
        let codelist = this.codelist;
        codelist[this.addressindex] = e.item.name.areaCode;
        this.codelist = codelist;

        this.datalist[this.addressindex] = e.item.name;
        // this.addressindex ==4 ? '': this.addressindex = this.addressindex + 1

        if (this.addressindex == 3 || this.addressindex == 2 && e.clickAreaCode.slice(6, 9) !== '000') {
          if (this.addressindex == 2 && e.clickAreaCode.slice(6, 9) !== '000' && (this.datalist[1].areaCode ==
              '441900000' || this.datalist[1].areaCode == '442000000' || this.datalist[1].areaCode == '460400000')) {
            this.datalist = this.datalist.slice(0, 3)
          }
          // 选择完毕后隐藏
          // this.popup = false;
          // this.choose_show = false;
        } else {
          this.addressindex = this.addressindex + 1;
          this.getcity(e.item.name.areaCode, e.item.name.areaType);
        }
      },
      closeMask() {
        this.popup = false;
      },
      clickCancel() {
        this.popup = false;
      },
      clickShowCityList(n) {
        let code = '';
        let nIndex = n
        this.flag = false
        this.addressindex = n;
        if (n == 0) {
          this.clickAreaCode = this.datalist[n].areaCode || -1
          this.clickAreaName = typeof(this.datalist[n]) == "object" ? this.datalist[n].areaName : ""
          code = '';
        } else {
          this.clickAreaCode = this.datalist[n].areaCode || -1
          this.clickAreaName = typeof(this.datalist[n]) == "object" ? this.datalist[n].areaName : ""
          code = this.codelist[nIndex - 1];
        }
        let hasChooseIndex = -1
        for (var i = 0; i < this.datalist.length; i++) {
          if (typeof(this.datalist[i]) == "string") {
            hasChooseIndex = i
            this.showIndex = i
            break
          }
        }
        this.showIndex = this.showIndex == -1 ? 3 : this.showIndex
        this.getcity(code, false);
      },
      // 点击请选择省市区区域
      ckSteps(n) {
        // 点击地址-》省市区
        let data_frist = ['请选择省', '请选择市', '请选择区', '请选择街道'];
        this.addressindex = n;
        let code = '';
        let nIndex = n
        if (this.datalist[0].areaCode == '0000000' && n !== 0) {
          this.datalist = ['请选择省', '请选择市', '请选择区', '请选择街道']
          this.codelist = []
          this.addressindex = 0
          n = 0
        }
        if (typeof(this.datalist[0]) == "object" && nIndex > 0 && typeof(this.datalist[0]) == "string") {
          this.datalist.forEach(function(item, index, array) {
            if (typeof(item) == "string" && typeof(array[index - 1]) == "object") {
              nIndex = index
            }
          })
        }
        if (typeof(this.datalist[n]) == 'string') {
          if (n == 0) {
            this.datalist = data_frist;
            this.codelist = [];
            code = '';
          } else {
            let datalist = this.datalist;
            datalist = datalist.splice(0, nIndex);
            data_frist = data_frist.splice(nIndex, data_frist.length);
            datalist = datalist.concat(data_frist);
            this.datalist = datalist;
            code = this.codelist[nIndex - 1];
          }
        } else {
          this.clickAreaCode = this.datalist[n].areaCode || -1
          this.clickAreaName = typeof(this.datalist[n]) == "object" ? this.datalist[n].areaName : ""
          if (this.clickAreaCode !== -1) {
            // this.codelist = [val.areaCode.slice(0,2) + '0000000', val.areaCode.slice(0,4) + '00000', val.areaCode.slice(0,6) + '000',]
            // this.datalist = [{
            //   'areaCode': val.areaCode.slice(0,2) + '0000000',
            //   'areaName': val.receiverState,
            //   'areaType': "state",
            //   'parentCode': "000000000"
            // }, {
            //   'areaCode': val.areaCode.slice(0,4) + '00000',
            //   'areaName': val.receiverCity,
            //   'areaType': "city",
            //   'parentCode': val.areaCode.slice(0,2) + '0000000'
            // }, {
            //   'areaCode': val.areaCode.slice(0,6) + '000',
            //   'areaName': val.receiverDistrict,
            //   'areaType': "district",
            //   'parentCode': val.areaCode.slice(0,4) + '00000'
            // }, '请选择街道']
          }
          code = this.codelist[n - 1]
        }
        this.getcity(code);
      },
      setOrgType(type) {
        this.type = type;
      },
      getMemberDetail: async function(jwt) {
        let that = this;
        const memberDetailsResult = await api.getMemberDetails({
          header: {
            jwt: jwt
          }
        });
        if (memberDetailsResult.code == 1000) {
          this.defaultAddress.receiverName = memberDetailsResult.data.fullName;
        } else {}
      },
      // 姓名
      receiverName(e) {
        this.defaultAddress.receiverName = e.detail.value;
        this.checkFormData();
      },

      // 手机号
      mobile(e) {
        this.defaultAddress.mobile = e.detail.value;
        this.checkFormData();
      },

      // 详细地址
      addressDetail(e) {
        this.defaultAddress.addressDetail = e.detail.value;
        this.checkFormData();
      },

      addAddress: async function(address) {
        let that = this;
        let customerInfo = uni.getStorageSync(CUSTOMER_INFO)
        let isDefult = 0;
        if (this.isDefult) {
          isDefult = 1;
        }
        let customerId = customerInfo.customerId;
        let jwt = uni.getStorageSync(CUSTOMER_INFO).jwt;
        const json = await api.saveAddress({
            method: 'POST',
            header: {
              jwt: jwt
            },
            query: {
              customerId: customerId,
              receiverMobile: address.mobile,
              receiverName: address.receiverName,
              receiverAddress: address.addressDetail,
              isDefault: isDefult,
              receiverState: that.datalist[0].areaName,
              receiverCity: that.datalist[1].areaName,
              receiverDistrict: that.datalist[2].areaName,
              receiverTown: that.datalist.length > 3 ? that.datalist[3].areaName : '',
              areaCode: that.datalist.length > 3 ? that.datalist[3].areaCode : that.datalist[2].areaCode
            }
          },
          customerId
        );
        this.address = Object.assign({}, address)
        this.address.receiverState = that.datalist[0].areaName
        this.address.receiverCity = that.datalist[1].areaName
        this.address.receiverDistrict = that.datalist[2].areaName
        this.address.receiverTown = that.datalist.length > 3 ? that.datalist[3].areaName : ''
        this.address.receiverAddress = address.addressDetail
        this.address.receiverMobile = address.mobile
        this.address.receiverTown = that.datalist.length > 3 ? that.datalist[3].areaName : ''
        if (json.code === 1000) {
          this.addressId = json.data.customerAddressId
		  if(this.type == 'reSet' ) {
			if (uni.getStorageSync('cartKey') !== null) {
				this.$emit('reSetAddressId', this.addressId);
			  return
			}
		  }
          uni.setStorageSync('addressId', json.data.customerAddressId);
          if (this.type == 'draw') {
            this.checkDistrict(json.data.customerAddressId)
            return false
          }
          this.datalist = ['请选择省', '请选择市', '请选择区', '请选择街道']
          this.codelist = []
          this.province = '';
          this.city = '';
          this.area = '';
          this.defaultAddress = {};
          this.isDefult = false;

          if (!this.showAddList && this.type == 'gift') {
            //0 列表 1新增 2编辑 (显示列表)
            this.getAddressInfo(json.data.customerAddressId)
            return
          }
          this.$emit('currentPage', 0, json.data.customerAddressId);
		 
        } else {
					this.handleShowTextToast(json.msg);
        }
        that.showLoading = false;
      },
      // 检测地区是否可以配送
      async checkDistrict(id) {
        let merchantInfo = uni.getStorageSync(MERCHANT_INFO) || getApp().globalData.merchantInfo;
        let customerInfo = uni.getStorageSync(CUSTOMER_INFO);
        const json = await api.checkDistrict({
          method: 'POST',
          header: {
            jwt: customerInfo.jwt
          },
          query: {
            storeId: merchantInfo.cloudMerchantStoreId,
            skuIds: this.skuIdArr,
            customerAddressId: id
          }
        })
        if (json.code == 1000) {
          this.$parent.checkDrawDistrict(this.addressId, 'add')
        } else {
          this.district = true
          this.delUserAddress(id)
        }
      },
	  
      openDialog() {
        this.dialog = true
        this.district = false
        this.$commonSensors.popupEvent('PopupExposure', {
          'popup_type': '购买',
          'popup_name': '确认登记信息弹窗'
        })
      },
      closeDialog() {
        this.dialog = false
        this.district = false
        this.delUserAddress(this.addressId)
        this.$commonSensors.popupEvent('PopupClick', {
          'popup_type': '购买',
          'popup_name': '确认登记信息弹窗',
          'button_name': '取消'
        })
      },
      openDistrictDialog(id) {
        this.district = true
        this.dialog = false
        this.delUserAddress(id)
      },
      closeDistrictDialog() {
        this.district = false
        this.dialog = false
      },
      delUserAddress: async function(id) {
        let that = this;
        let customerInfo = uni.getStorageSync(CUSTOMER_INFO)
        const json = await api.delUserAddress({
            method: 'DELETE',
            header: {
              jwt: customerInfo.jwt
            }
          },
          customerInfo.customerId,
          id
        );
        if (json.code === 1000) {
          uni.removeStorageSync('addressId');
        }
      },
      //用户收货地址
      getAddressInfo: async function(id) {
        let customerInfo = uni.getStorageSync(CUSTOMER_INFO)
        let customerId = customerInfo.customerId;
        let jwt = uni.getStorageSync(CUSTOMER_INFO).jwt;
        const json = await api.getAddressInfoById({
            header: {
              jwt: jwt
            }
          },
          customerId,
          id
        );
        if (json.code === 1000) {
          this.$emit('setDeafaultAddress', {
            info: json.data,
            type: 'add'
          })
        } else {
					this.handleShowTextToast(json.msg);
        }
      },
      changeCurrent() {
        this.$emit('currentPage', 0);
      },

      // 默认地址
      changeCheckBoxState() {
        this.isDefult = !this.isDefult;
      },
      stopClick() {
        return false
      },
      //  表单校验
      checkFormData() {
        this.defaultAddress.receiverName = !!this.defaultAddress.receiverName ? this.defaultAddress.receiverName
          .trim() : this.defaultAddress.receiverName;
        this.defaultAddress.receiverName = this.defaultAddress.receiverName && this.defaultAddress.receiverName.replace(
          /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]|[\uD800-\uDBFF]|[\uDC00-\uDFFF]|[^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n]/g,
          ''
        );
        this.defaultAddress.addressDetail = this.defaultAddress.addressDetail && this.defaultAddress.addressDetail
          .replace(
            /\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]|[\uD800-\uDBFF]|[\uDC00-\uDFFF]|[^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n]/g,
            ''
          );

        if (!this.defaultAddress.receiverName) {
          this.activityBtnShow = false;
          return {
            success: false,
            msg: "输入收件人姓名"
          };
        }
        if (!this.defaultAddress.mobile) {
          this.activityBtnShow = false;
          return {
            success: false,
            msg: "输入联系电话"
          };
        } else if (this.defaultAddress.mobile.length < 11) {
          this.activityBtnShow = false;
          return {
            success: false,
            msg: "手机号长度有误"
          };
        }
        const myreg = /^(1[3|4|5|6|7|8|9])\d{9}$/;
        if (!myreg.test(this.defaultAddress.mobile)) {
          this.activityBtnShow = false;
          return {
            success: false,
            msg: "手机号格式有误"
          };
        }

        if (this.datalist.length == 4 && typeof(this.datalist[3]) !== "object" || this.datalist.length == 3 && typeof(
            this.datalist[2]) !== "object") {
          this.activityBtnShow = false;
          return {
            success: false,
            msg: "请完善所在地区"
          };
        }
        if (!this.defaultAddress.addressDetail) {
          this.activityBtnShow = false;
          return {
            success: false,
            msg: "请输入详细地址信息"
          };
        }
        this.activityBtnShow = true;
        return {
          success: true
        };
      },
      formSubmit() {
        const { success, msg } = this.checkFormData();
				if (!success) return this.handleShowTextToast(msg);
				if(this.type == 'reSet' ) {
					this.$emit('reSetAddModel', ()=>{ this.addAddress(this.defaultAddress) });
				}else{
					this.addAddress(this.defaultAddress);
				}
      },
      openAddressPicker() {
        this.$refs.areaPicker.openAddressPicker();
      },
      openAddressPickerNew(e) {
        let hasChooseIndex = -1
        for (var i = 0; i < this.datalist.length; i++) {
          if (typeof(this.datalist[i]) == "string") {
            hasChooseIndex = i
            this.showIndex = i
            break
          }
        }
        let maxIndex = this.datalist.length == 3 ? 2 : 3
        this.showIndex = this.showIndex == -1 ? maxIndex : this.showIndex
        let chooseIdx = hasChooseIndex == -1 ? e.currentTarget.dataset.index : hasChooseIndex
        // debugger
        if (this.datalist.length == 3 && chooseIdx == 3) {
          this.clickAreaName = typeof(this.datalist[chooseIdx - 1]) == "object" ? this.datalist[chooseIdx - 1]
            .areaName : ""
          this.clickAreaCode = this.datalist[chooseIdx - 1].areaCode || -1

          this.ckSteps(chooseIdx - 1)
        } else {
          this.clickAreaName = typeof(this.datalist[chooseIdx]) == "object" ? this.datalist[chooseIdx].areaName : ""
          this.clickAreaCode = this.datalist[chooseIdx].areaCode || -1
          this.ckSteps(chooseIdx)
        }

        this.popup = true
      },

      areaPickerArray(province, city, area) {
        this.province = province;
        this.city = city;
        this.area = area;
        this.provinceName = province.areaName;
        this.cityName = city.areaName;
        this.areaName = area.areaName;
        this.provinceCode = province.areaCode;
        this.cityCode = city.areaCode;
        this.areaCode = area.areaCode;
      },

      getAddress() {
        let that = this;
        uni.getSetting({
          success(res) {
            if (res.authSetting['scope.address']) {
              uni.chooseAddress({
                success(res) {
                  that.setDefaultAddress(res);
                }
              });
              // 用户已经同意小程序使用录音功能，后续调用 uni.startRecord 接口不会弹窗询问
            } else {
              if (res.authSetting['scope.address'] == false) {
                uni.openSetting({
                  success(res) {
             
                  }
                });
              } else {
                uni.chooseAddress({
                  success(res) {
                    that.setDefaultAddress(res);
                  }
                });
              }
            }
          }
        });
      },

      setDefaultAddress(address) {
        this.defaultAddress.receiverName = address.userName;
        this.defaultAddress.mobile = address.telNumber;
        this.defaultAddress.addressDetail = address.detailInfoNew || address.detailInfo;
        this.province = {
          areaName: address.provinceName
        };
        this.city = {
          areaName: address.cityName
        };
        this.area = {
          areaName: address.countyName
        };
        this.provinceName = address.provinceName;
        this.cityName = address.cityName;
        this.areaName = address.countyName;
        if (address.nationalCode.slice(0, 4) == '4419' || address.nationalCode.slice(0, 4) == '4420' || address
          .nationalCode.slice(0, 4) == '4604') {
          this.showIndex = 2
          this.clickAreaCode = address.nationalCodeFull
          this.clickAreaName = address.countyName
          this.codelist = [address.nationalCode.slice(0, 2) + '0000000', address.nationalCode.slice(0, 4) + '00000',
            address.nationalCode.slice(0, 6) + '000',
          ]
          this.datalist = [{
            'areaCode': address.nationalCode.slice(0, 2) + '0000000',
            'areaName': address.provinceName,
            'areaType': "state",
            'parentCode': "000000000"
          }, {
            'areaCode': address.nationalCode.slice(0, 4) + '00000',
            'areaName': address.cityName,
            'areaType': "city",
            'parentCode': address.nationalCode.slice(0, 2) + '0000000'
          }, {
            'areaCode': address.nationalCodeFull,
            'areaName': address.countyName,
            'areaType': "district",
            'parentCode': address.nationalCode.slice(0, 4) + '00000'
          }]
        } else {
          // this.clickAreaName = address.streetName
          if (address.nationalCode.slice(6, 9) !== '000') {
            this.showIndex = 3
            this.clickAreaCode = address.nationalCodeFull
          }
          this.codelist = [address.nationalCode.slice(0, 2) + '0000000', address.nationalCode.slice(0, 4) + '00000',
            address.nationalCode.slice(0, 6) + '000',
          ]
          this.datalist = [{
            'areaCode': address.nationalCode.slice(0, 2) + '0000000',
            'areaName': address.provinceName,
            'areaType': "state",
            'parentCode': "000000000"
          }, {
            'areaCode': address.nationalCode.slice(0, 4) + '00000',
            'areaName': address.cityName,
            'areaType': "city",
            'parentCode': address.nationalCode.slice(0, 2) + '0000000'
          }, {
            'areaCode': address.nationalCode.slice(0, 6) + '000',
            'areaName': address.countyName,
            'areaType': "district",
            'parentCode': address.nationalCode.slice(0, 4) + '00000'
          }]
          if (address.streetName) {
            this.datalist.push({
              'areaCode': address.nationalCodeFull,
              'areaName': address.streetName,
              'areaType': "town",
              'parentCode': address.nationalCode.slice(0, 6) + '000'
            })
          } else {
            this.datalist.push('请选择街道');
          }
        }
      },
      checkIn() {
        this.$parent.checkIn(this.addressId, 'add')
        this.dialog = false
      }
    }
  };
</script>
<style lang="less">
  .address_add_box {
    background: #fff;
    padding: 0 48rpx;
    width: 100%;
    font-family: FrankNew-Regular, NotoSansSC-Regular;
  }

  .text_font {
    font-family: FrankNew-Regular, NotoSansSC-Regular;
  }

  .text_fontnum {
    font-family: FrankNew-Regular;
  }

  .list_item_address {
    height: auto !important;
    padding: 48rpx 0 !important;
  }

  .list_item {
    display: flex;
    align-items: center;
    border-bottom: 1px solid #f2f2f2;
    height: 144rpx;

    .title {
      width: 230rpx;
      height: 64rpx;
      line-height: 64rpx;
      font-size: 28rpx;
      color: #1a1a1a;
    }

    .input {
      flex: 1;
      text-align: right;
      min-height: 64rpx;
      line-height: 64rpx;
      display: flex;
      align-items: center;
      justify-content: flex-end;

      input {
        width: 100%;
        height: 64rpx;
        line-height: 64rpx;
        color: #757575;
        font-size: 24rpx;
      }
    }
  }

  .btn_box {
    margin-top: 20rpx;

    // &.giftBtn {
    //   position: fixed;
    //   left: 0;
    //   right: 0;
    //   bottom: 30rpx;
    //   margin: 0 24rpx;
    //   padding: 0;
    // }
    .btnStyle {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 654rpx;
      height: 80rpx;
      border-radius: 48rpx 16rpx 48rpx 48rpx;
      font-size: 28rpx;
      color: #ffffff;
      background-color: #000;
      padding: 0;
    }
  }

  .btnStyle_not {
    background-color: #c6c6c6 !important;
    color: #757575 !important;
  }

  .qAdd {
    width: 100%;
    height: 138rpx;
    text-align: center;
    font-family: NotoSansSC-Regular;
    font-size: 28rpx;
    font-weight: 500;
    color: #000;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .qAdd_text {
    border-bottom: 1px solid #000;
    padding-bottom: 6px;
    text-align: center;
    line-height: 1;
  }

  .tipBox {
    font-size: 20rpx;
    color: #757575;

    .labeltext {
      margin-top: 10rpx;
      text-align: justify;
      font-size: 20rpx;
      color: #757575;
    }
  }

  .topView {
    position: fixed;
    width: 100%;
    height: 100%;
    top: 0;
    background-color: rgba(0, 0, 0, 0.2);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10;

    .address-block {
      margin-bottom: 30rpx;
    }

    .centerBox {
      width: 520rpx;
      background-color: #fff;

      .title {
        text-align: center;
        font-size: 28rpx;
        padding: 24rpx 0;
        background-color: #000;
        color: #fff;
      }

      .content {
        padding: 30rpx 36rpx 0rpx;
        // height: 450rpx;
        overflow: auto;
        text-align: justify;
        margin-bottom: 28rpx;

        .addressTitle {
          font-size: 24rpx;
        }

        .font26 {
          font-size: 26rpx;
        }

        .font18 {
          padding: 0;
          font-size: 22rpx;
          line-height: 38rpx;
          margin-bottom: 6rpx;
          color: #CB171A;
        }

      }

      .btnBox {
        display: flex;
        justify-content: space-around;
        margin-bottom: 40rpx;
        padding: 0 55rpx;

        .btn {
          padding: 14rpx 0;
          width: 135rpx;
          /* height:54rpx; */
          text-align: center;
          border: 2rpx solid #000;
          background-color: #fff;
          color: #1A1A1A;
          font-size: 26rpx;
          line-height: 30rpx;
          margin: 0;

          &.submit {
            border: 2rpx solid #000;
            color: #fff;
            background-color: #000;
            height: 60rpx;
          }
        }
      }
    }
  }

  .user_info {
    text {
      margin-right: 10rpx;
    }
  }

  .mask {
    width: 100%;
    height: 100%;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: #1b1b1b2b;
    z-index: 0;
  }

  .content_box {
    width: 100%;
    height: 1200rpx;
    /* height: 600upx; */
    background: white;
    z-index: 11;
    position: fixed;
    bottom: 0;
    left: 0;
  }

  .separate-tag {
    padding: 0 10rpx;
  }

  .add-address-mask {
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    z-index: 9999;
    background: rgba(0, 0, 0, 0.7);
  }

  .max-height {
    height: 621rpx;
  }

  .sec-height {
    height: 539rpx;
  }

  .third-height {
    height: 455rpx;
  }

  .fouth-height {
    height: 368rpx;
  }

  .defult_img {
    width: 32rpx;
    height: 32rpx;
    margin: 16rpx;
  }

  .line_img {
    width: 100%;
    height: 12rpx;
    margin-top: 48rpx;
  }

  .weixin_get_address {
    width: 100%;
    height: 138rpx;
  }

  .right_icon {
    width: 64rpx;
    height: 64rpx;
    margin-left: 16rpx;
  }

  .bottom_box {
    width: 100%;
    position: fixed;
    left: 0;
    bottom: 64rpx;
    padding: 0 48rpx;
  }
</style>
